package com.sjb.sixteen_journey_backend.service.impl;

import com.sjb.sixteen_journey_backend.common.R;
import com.sjb.sixteen_journey_backend.entity.MapRoute;
import com.sjb.sixteen_journey_backend.mapper.MapRouteMapper;
import com.sjb.sixteen_journey_backend.service.IMapRouteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjb.sixteen_journey_backend.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 路线表 服务实现类
 * </p>
 *
 * @author jcz
 * @since 2024-07-26
 */
@Service
public class MapRouteServiceImpl extends ServiceImpl<MapRouteMapper, MapRoute> implements IMapRouteService {

    @Override
    public R addMapRoute(Integer id,List<Integer> ids) {
        try {
            MapRoute mapRoute = new MapRoute();
            String idStr = ids.stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
            Map<String, Object> map = ThreadLocalUtil.get();
            Integer userId= (Integer) map.get("id");
            mapRoute.setUserId(userId);
            mapRoute.setMyMapId(id);
            mapRoute.setSiteIdList(idStr);
            if(this.save(mapRoute)){
                return R.success(null,"地图路线添加成功");
            }

            return R.error("地图路线添加失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public R removeMapRoute(Integer id) {
        try {
            if(this.getById(id)!=null || this.removeById(id)) return R.success(null,"删除地图路线成功");
            return R.error("删除地图路线失败");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
